#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include 
string str;
void print_(TreeNode* root)
{
    if (!root)
        return;
    if (root->right == nullptr && root->left)
    {
        str.push_back('(');
        str.push_back(root->val + '0');
        str.push_back(')');
        return;
    }
    if (root->left == nullptr)
    {
        str.push_back('(');
        str.push_back(')');
        print_(root->right);
        return;
    }
    else {
        str.push_back('(');
        str.push_back(root->val + '0');
        print_(root->left);
        str.push_back(')');
        return;
    }
}

class Solution {
public:
    string tree2str(TreeNode* root) {
        str.push_back(root->val + '0');
        print_(root->left);
        print_(root->right);
        return str;
    }
};